﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Forms;
using Shoozla.ViewModel;


namespace Shoozla
{
    public partial class FolderExplorerControl : System.Windows.Controls.UserControl
    {
        private readonly object _dummyNode = null;
        public event SelectedFolderChangedDelegate SelectedFolderChanged;
        public delegate void SelectedFolderChangedDelegate(string folderName);

        public FolderExplorerControl()
        {
           InitializeComponent();
           DataContext = new FolderExplorerViewModel();
           foldersTree.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(foldersTree_SelectedItemChanged);
        }

        void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == _dummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (DirectoryInfo subFolder in ((DirectoryInfo)item.Tag).EnumerateDirectories())
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = subFolder.Name;
                        subitem.Tag = subFolder;
                        subitem.Items.Add(_dummyNode);
                        subitem.Expanded += folder_Expanded;
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }

        void foldersTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (SelectedFolderChanged != null)
            {
                TreeViewItem item = (TreeViewItem)foldersTree.SelectedItem;
                DirectoryInfo selectedFolder = (DirectoryInfo)item.Tag;
                SelectedFolderChanged.Invoke(selectedFolder.FullName);
            }
        }

        public void AddFolderToView(string folder)
        {
            try
            {
                if (!String.IsNullOrEmpty(folder))
                {
                    //add the item as root
                    DirectoryInfo mainFolder = new DirectoryInfo(folder);
                    TreeViewItem mainItem = new TreeViewItem();
                    mainItem.Header = mainFolder.Name;
                    mainItem.Tag = mainFolder;
                    mainItem.Items.Add(_dummyNode);
                    mainItem.Expanded += folder_Expanded;

                    // Apply the attached property so that 
                    // the triggers know that this is root item.
                    TreeViewItemProps.SetIsRootLevel(mainItem, true);
                    foldersTree.Items.Add(mainItem);

                }
            }
            catch (UnauthorizedAccessException accEx)
            {
                App.LogEntry("[ERROR] AddFolderToView: " + accEx.Message);
            }
        }

    }
}
